<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="2" labelWidth="120" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="inOutReport11w"
      :columns="columns"
      :params="fetchParams"
      :onRowSelectChange="rowSelectHandle"
      :isMemoryPagination="true"
      :dataSource="dataSource"
      selectionType="multiple"
      :onColumnsChange="columns => (this.columns = columns)"
      :isSelectColumn="true"
      :onSyncTableData="tableHandle"
      :defaultSelections="defaultSelections"
    >
      <!-- :fetchapi="fetchApi" -->
      <!-- :datakey="'records'" -->
      <template slot="controls">
        <el-button size="small" type="primary" @click="addRecordHandle">保存</el-button>
      </template>
    </FilterTable>
  </div>
</template>

<script>
/*
 *
 * 查询配件物流查询报表
 */

import { authority } from '@/utils/authMixin';
import { logisticsQueryInfo, saveComplaint } from '@/api/part/report/logisticsInquiry';
import moment from 'moment';
export default {
  name: 'inOutReport',
  components: {},
  mixins: [authority],
  data() {
    this.selectedRow = [];
    return {
      visible: false,
      topFilterList: this.createTopFilters(),
      fetchApi: logisticsQueryInfo,
      columns: this.createTableColumns(),
      fetchParams: { xhrAbort: true },
      formData: {}, // 向子组件传数据的数组
      tableData: [], // 表单数据
      defaultSelections: [],
      dataSource: []
    };
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    // this.initQuery();
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '销售申请号',
          fieldName: 'I_QVBELN',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '发货单号',
          fieldName: 'I_DVBELN',
          placeholder: '请输入...'
        },
        {
          type: 'RANGE_DATE',
          label: '实际发货日期',
          dateType: 'daterange',
          initialValue: [
            moment()
              .subtract(2, 'months')
              .format('YYYY-MM-DD'),
            moment().format('YYYY-MM-DD')
          ],
          // initialValue: [new Date() - 1000 * 60 * 60 * 24 * 60, new Date()],
          dateFormat: 'yyyy-MM-dd',
          style: { minWidth: '120px' },
          fieldName: 'I_DATES|I_DATEE'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'LINNO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售申请号',
          dataIndex: 'QVBLN',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发货单号',
          dataIndex: 'DVBLN',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '经销商',
          dataIndex: 'KUNNR',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'NAME',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发货点',
          dataIndex: 'LGORT',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发运方式',
          dataIndex: 'VASRT',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发运方式描述',
          dataIndex: 'MS',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库',
          dataIndex: 'SLGORT',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发货单打印日期',
          dataIndex: 'PRDAT',
          align: 'center',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发货单打印时间',
          dataIndex: 'PRZET',
          align: 'center',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '预计到达日期',
          dataIndex: 'YRDAT',
          align: 'center',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '到货日期',
          dataIndex: 'DERDAT',
          align: 'center',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '到货时间',
          dataIndex: 'DERZET',
          align: 'center',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '实际发货日期',
          dataIndex: 'SHDAT',
          align: 'center',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '辖区内发货',
          dataIndex: 'ZDOMN',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '紧急订单',
          dataIndex: 'ZFLAG',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '到货确认人',
          dataIndex: 'DERNAM',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件到达时损坏',
          dataIndex: 'CHECK2',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '投诉',
          dataIndex: 'TSBS',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '投诉原因',
          dataIndex: 'TS',
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 搜索
    changeHandle(val) {
      // this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({ ImportParameter: val }, val);
      this.inquiry(this.fetchParams);
      // this.columns = [...this.columns];
    },
    async inquiry(val) {
      // console.log(val, '查询参数');

      let res = await logisticsQueryInfo(val);
      this.dataSource = res.data;
      // this.$refs.table.EXECUTE_INSERT(res.data);
    },
    // 初始化查询
    async initQuery() {
      let [err, formData] = await this.$refs.topFilter.GET_FORM_DATA();
      let fiterData = formData;
      this.fetchParams = Object.assign(this.fetchParams, fiterData[1], {
        ImportParameter: fiterData[1],
        xhrAbort: false
      });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 校验订单是否发货
    checkIsShipped() {
      this.selectedRow.forEach(element => {
        if (element.VASRT == '' || element.VASRT == null || element.VASRT == undefined) {
          this.BaseTable.SET_DISABLE_SELECT(element);
          throw this.$notify.error({ title: '提示信息', message: '该订单未发货' });
        }
      });
    },
    // 保存方法
    async addRecordHandle() {
      if (this.selectedRow.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选择一条数据' });
      }
      try {
        this.checkIsShipped();
      } catch (error) {
        // 取消选中
        this.defaultSelections = [];
        return;
      }
      let params = {};
      params.TableParameter = {};
      params.ImportParameter = { I_WXZ: '' };
      // this.selectedRow.forEach(element => {
      //   element.VASRT = '01';
      // });
      params.TableParameter.IT_WL_SAVE = this.selectedRow;
      const res = await saveComplaint(params);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '保存成功!' });
        this.fetchParams = { ...this.fetchParams };
        // 需要主页面刷新
        this.closeHandler('refresh');
      }
    },
    // table动态回调函数
    tableHandle(val) {
      this.tableData = val;
      console.log(this.tableData);
    },
    rowSelectHandle(rows) {
      this.selectedRow = [];
      if (!rows.length) return;
      this.selectedRow = rows;
    },

    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
